package com.fingard.dsp.bank.directbank.ccb02;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlReadMode;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class CCB02BatPayRec extends CCB02Base implements IBatPayRec {
    //1988
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    //9188
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return d_BatPayRec(p_req9188);
    }

    //批量代收和批量代付提交
    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
        //返回ATS的对象
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            /*
             * <!--
  	atsAct为ATS账户号，isPrivate为公私标志 ，useType为ATS的代收付用途
  	上面三个条件，条件配置为*则代表全部，映射下面的结果
  	billCode为批量项目编号，billDesc代发代扣项目名称，useofCode批量用途编号-->
			 * */
            String[] srcNames = new String[]{"atsAct", "isPrivate", "useType"};
            String[] trgNames = new String[]{"billCode", "billDesc", "useofCode"};
            String[] srcValues = new String[]{p_reqBat.ownAct, p_reqBat.isPrivate, p_reqBat.useType};
            HashMap<String, String> tmpMap = getBankConfig().getMapAdvanceValues("billCode" + p_reqBat.reqHeader.transType, srcNames, trgNames, srcValues);
            if (tmpMap == null || tmpMap.size() != 3) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到相应的代发代扣编号设置";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = "E9999";
                retAts.writeRetToATS();
                return retAts;
            }
            StringBuilder tmpSbData = new StringBuilder();
            int tmpCount = 0;

            List<HashMap<String, String>> batPayFile = getBankConfig().getListHashMap("batPayFile", new String[]{"key", "value", "des"}, new String[]{"key", "value", "des"});
            if (Integer.valueOf(p_reqBat.totalNum) > 0) {
                for (int i = 1; i <= batPayFile.size(); i++) {
                    for (HashMap<String, String> trgMap : batPayFile) {
                        if (trgMap.get("key").equals(i + "")) {
                            tmpSbData.append(trgMap.get("des") + "|");
                        }
                    }
                }
                tmpSbData.append("\r\n");
            }
            while (p_reqBat.readRecord()) {
                tmpCount++;
                for (int i = 1; i <= batPayFile.size(); i++) {
                    for (HashMap<String, String> trgMap : batPayFile) {
                        if (trgMap.get("key").equals(i + "")) {
                            if (trgMap.get("value").equals("SameBnk")) {
                                if (p_reqBat.readedDetail.sameBnk != null && p_reqBat.readedDetail.sameBnk.equals("1")) {
                                    tmpSbData.append("0" + "|");
                                } else {
                                    tmpSbData.append("1" + "|");
                                }
                            } else if (trgMap.get("value").equals("Index")) {
                                tmpSbData.append(tmpCount + "|");
                            } else if (trgMap.get("value").equals("RecAct") || trgMap.get("value").equals("RecName") || trgMap.get("value").equals("PayAct") || trgMap.get("value").equals("PayName")) {
                                if (p_reqBat.reqHeader.transType.equals("1988")) {
                                    if (trgMap.get("value").equals("RecAct") || trgMap.get("value").equals("PayAct")) {
                                        tmpSbData.append(p_reqBat.readedDetail.recAct + "|");
                                    }
                                    if (trgMap.get("value").equals("RecName") || trgMap.get("value").equals("PayName")) {
                                        tmpSbData.append(p_reqBat.readedDetail.recName + "|");
                                    }
                                } else if (p_reqBat.reqHeader.transType.equals("9188")) {
                                    if (trgMap.get("value").equals("RecAct") || trgMap.get("value").equals("PayAct")) {
                                        tmpSbData.append(p_reqBat.readedDetail.payAct + "|");
                                    }
                                    if (trgMap.get("value").equals("RecName") || trgMap.get("value").equals("PayName")) {
                                        tmpSbData.append(p_reqBat.readedDetail.payName + "|");
                                    }
                                }
                            } else if ("".equals(trgMap.get("value"))) {
                                tmpSbData.append("|");
                            } else {
                                tmpSbData.append(p_reqBat.readedDetail.srcXElement.getSingleText(trgMap.get("value")) + "|");
                            }
                        }
                    }
                }
                tmpSbData.append("\r\n");
            }
// region
//            //区分新旧上传接口
//            if ("1".equals(getBankConfig().getValueAsString("useNewPayrollType")) && p_reqBat.reqHeader.transType.equals("1988")) {
//                // #start 上传数据文件
//                //{序号} {账号} {户名} {金额} {跨行标识} {行名} {联行行号} {摘要 } {备注}
//                if (Integer.valueOf(p_reqBat.totalNum) > 0) {
//                    tmpSbData.append("序号|");
//                    tmpSbData.append("账号|");
//                    tmpSbData.append("户名|");
//                    tmpSbData.append("金额|");
//                    tmpSbData.append("跨行标识|");
//                    tmpSbData.append("行名|");
//                    tmpSbData.append("联行行号|");
//                    tmpSbData.append("摘要|");
//                    tmpSbData.append("备注" + "\r\n");
//                }
//                while (p_reqBat.readRecord()) {
//
//                    tmpCount++;
//                    tmpSbData.append(String.valueOf(tmpCount) + "|");
//
//
//                    tmpSbData.append(p_reqBat.readedDetail.recAct + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.srcXElement.getSingleText("RecName") + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.payAmount + "|");
//                    if (p_reqBat.readedDetail.sameBnk != null && p_reqBat.readedDetail.sameBnk.equals("1")) {
//                        tmpSbData.append("0" + "|");
//                    } else {
//                        tmpSbData.append("1" + "|");
//                    }
//                    tmpSbData.append(p_reqBat.readedDetail.recBankName + "|");
//
//                    tmpSbData.append(p_reqBat.readedDetail.cnapsCode + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.askNumber + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.abstractATS + "\r\n");
//                }
//            } else if ("2".equals(getBankConfig().getValueAsString("useNewPayrollType")) && p_reqBat.reqHeader.transType.equals("1988")) {
//                // #start 上传数据文件
//                //{序号} {账号} {户名} {金额} {跨行标识} {联行行号} {多方协议号} {摘要 } {备注}
//                if (Integer.valueOf(p_reqBat.totalNum) > 0) {
//                    tmpSbData.append("序号|");
//                    tmpSbData.append("账号|");
//                    tmpSbData.append("户名|");
//                    tmpSbData.append("金额|");
//                    tmpSbData.append("跨行标识|");
//                    tmpSbData.append("联行行号|");
//                    tmpSbData.append("多方协议号|");
//                    tmpSbData.append("摘要|");
//                    tmpSbData.append("备注" + "\r\n");
//                }
//                while (p_reqBat.readRecord()) {
//                    tmpCount++;
//                    tmpSbData.append(String.valueOf(tmpCount) + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.recAct + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.recName + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.payAmount + "|");
//                    if (p_reqBat.readedDetail.sameBnk != null && p_reqBat.readedDetail.sameBnk.equals("1")) {
//                        tmpSbData.append("0" + "|");
//                    } else {
//                        tmpSbData.append("1" + "|");
//                    }
//
//                    tmpSbData.append(p_reqBat.readedDetail.cnapsCode + "|");
//                    tmpSbData.append("|");
//                    tmpSbData.append(p_reqBat.readedDetail.askNumber + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.abstractATS + "\r\n");
//                }
//            } else {
//                // #start 上传数据文件
//                //{编号}|{账号}|{户名}|{金额}|{摘要}|{备注1}|{备注2}
//                while (p_reqBat.readRecord()) {
//                    tmpCount++;
//                    tmpSbData.append(String.valueOf(tmpCount) + "|");
//                    if (p_reqBat.reqHeader.transType.equals("1988")) {
//                        tmpSbData.append(p_reqBat.readedDetail.recAct + "|");
//                        tmpSbData.append(p_reqBat.readedDetail.recName + "|");
//                    } else if (p_reqBat.reqHeader.transType.equals("9188")) {
//                        tmpSbData.append(p_reqBat.readedDetail.payAct + "|");
//                        tmpSbData.append(p_reqBat.readedDetail.payName + "|");
//                    }
//                    tmpSbData.append(p_reqBat.readedDetail.payAmount + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.askNumber + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.purpose + "|");
//                    tmpSbData.append(p_reqBat.readedDetail.abstractATS + "\r\n");
//                }
//            }
// endregion
            this.WriteBankLogLn2(tmpSbData.toString());
            String tmpDataFileName = p_reqBat.reqBatchNO + ".txt";
            String tmpFilePath = StringHelper.trimEnd(bankFront.bkPayFolder, File.separator) + File.separator + tmpDataFileName;
            FileHelper.writeFile(tmpFilePath, tmpSbData);

            String rHostFolder = "";
            if (StringHelper.hasAnyChar(bankFront.folderForRemoteHost)) {
                rHostFolder = StringHelper.trimEnd(bankFront.folderForRemoteHost, File.separator) + File.separator;
            }
            String fileForRemoteHost = rHostFolder + tmpDataFileName;
            StringBuilder tmpSbUp = new StringBuilder();
            String uploadedFile = uploadFileToBank(fileForRemoteHost, tmpSbUp, p_reqBat);
            if (uploadedFile.length() == 0) {
                retAts.respCode = "0";
                retAts.respInfo = tmpSbUp.toString();
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = "E9999";
                retAts.writeRetToATS();
                return retAts;
            }
            // #end

            CCB02Header bankHeader = new CCB02Header();
            bankHeader.TX_CODE = "6W2100";
            bankHeader.REQUEST_SN = codeToAscii(p_reqBat.reqBatchNO);
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("TX_INFO");

            //billCode为批量项目编号，billDesc代发代扣项目名称，useofCode批量用途编号
            xmlWriter.writeElementString("BILL_CODE", tmpMap.get("billCode"));
            xmlWriter.writeElementString("BILLDESC", tmpMap.get("billDesc"));

            if (p_reqBat.reqHeader.transType.equals("1988")) {//0 代发（空 默认代发） 1 代扣
                xmlWriter.writeElementString("BILL_FLAG", "0");
            } else if (p_reqBat.reqHeader.transType.equals("9188")) {
                xmlWriter.writeElementString("BILL_FLAG", "1");
            }
            xmlWriter.writeElementString("USEOF_CODE", tmpMap.get("useofCode"));
            if (!StringHelper.isNullOrEmpty(p_reqBat.useType)) {
                xmlWriter.writeElementString("USE", p_reqBat.useType);
            } else {
                String payUse = getBankConfig().getMapValueByKey("payUseMapping9188", p_reqBat.ownAct);
                if (StringHelper.isNullOrEmpty(payUse)) {
                    retAts.respCode = "0";
                    retAts.respInfo = "ATS上送UseType或Usage字段为空的同时，config.xml文件中payUseMapping9188节点不存在或者key为" + p_reqBat.ownAct + "的用途不存在";
                    retAts.reqSeqID = p_reqBat.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = "E9999";
                    retAts.writeRetToATS();
                    return retAts;
                }
                xmlWriter.writeElementString("USE", payUse);
            }
            xmlWriter.writeElementString("SEND_FILE", uploadedFile);
            xmlWriter.writeElementString("ORI_FILE", tmpDataFileName);
            xmlWriter.writeElementString("ACC_NO", p_reqBat.ownAct);
            xmlWriter.writeElementString("AMOUNT", p_reqBat.totalAmt);
            xmlWriter.writeElementString("COUNT", p_reqBat.totalNum);
            xmlWriter.writeElementString("FLOW_FLAG", "0");
            if (p_reqBat.getTotalNum() > 50000) {
                xmlWriter.writeElementString("ASYN_FLAG", "1");
            } else {
                xmlWriter.writeElementString("ASYN_FLAG", "0");
            }
            if (p_reqBat.getTotalNum() > 5000) {
                xmlWriter.writeElementString("RET_ERRTYPE", "0");
            } else {
                xmlWriter.writeElementString("RET_ERRTYPE", "1");
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeElementClosed("SIGN_INFO");
            xmlWriter.writeElementClosed("SIGNCERT");

            xmlWriter.writeEndDocument();//写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String RETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    String RETURN_MSG = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                    retAts.respCode = "0";
                    retAts.reqSeqID = p_reqBat.reqBatchNO;//tmpBkReader.getFirstText("/TX/REQUEST_SN");
                    retAts.transState = RETURN_CODE;
                    retAts.payInfoCode = RETURN_CODE;
                    retAts.respInfo = RETURN_MSG;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    private String uploadFileToBank(String p_filePath, StringBuilder p_sb, ReqBatPayRec p_reqBat) throws Exception {
        CCB02Header bankHeader = new CCB02Header();
        bankHeader.REQUEST_SN = this.getQryTransID();
        bankHeader.TX_CODE = "6W2102";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("TX_INFO");
        xmlWriter.writeElementString("SEND_FILE", p_filePath);
        xmlWriter.writeEndDocument();
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_reqBat.reqHeader.transType, p_reqBat.reqHeader.transType);//发送银行
        if (tmpStrRet[0].length() == 0) {
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], p_sb);
            if (tmpBkReader != null) {
                String RETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                String RETURN_MSG = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                if (RETURN_CODE.equals("000000")) {
                    String SEND_FILE = tmpBkReader.getFirstText("/TX/TX_INFO/SEND_FILE");
                    if (StringHelper.hasAnyChar(SEND_FILE)) {
                        return SEND_FILE;
                    }
                } else {
                    p_sb.append(RETURN_MSG);
                }
            }
        } else {
            p_sb.append(tmpStrRet[1]);
        }
        return "";
    }


    //1998 状态查询
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    //9198
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return d_QryBatPayRec(p_req9198);
    }

    //批量代收和批量代付状态查询
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        //返回ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.respCode = "";
        CCB02Header bankHeader = new CCB02Header();
        bankHeader.REQUEST_SN = p_reqQryBat.reqHeader.transSeqID;
        bankHeader.TX_CODE = "6W2104";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeStartElement("TX_INFO");
        xmlWriter.writeElementString("REQUEST_SN1", codeToAscii(p_reqQryBat.reqBatchNO));
        xmlWriter.writeEndDocument();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String RETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    String RETURN_MSG = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                    if (RETURN_CODE.equals("000000")) {
                        //0：处理失败  1：处理完成  2：处理中
                        String tmpF_STATUS = tmpBkReader.getFirstText("/TX/TX_INFO/F_STATUS");
                        String tmpF_MSG = tmpBkReader.getFirstText("/TX/TX_INFO/F_MSG");


                        String tmpSUCCESS_NUM = tmpBkReader.getFirstText("/TX/TX_INFO/SUCCESS_NUM");
                        String tmpFAIL_NUM = tmpBkReader.getFirstText("/TX/TX_INFO/FAIL_NUM");

                        int tmpSuccCount = tmpBkReader.countElement("/TX/TX_INFO/SUCCESS_LIST");
                        if (tmpF_STATUS.equals("2")) {
                            retAts.transState = bankHeader.TX_CODE + "+" + tmpF_STATUS;
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                            retAts.payInfoCode = tmpF_STATUS;
                            retAts.respInfo = tmpF_MSG;
                            retAts.writeBeforeDetail();
                        } else if (tmpF_STATUS.equals("0") && tmpSUCCESS_NUM.length() == 0 && tmpSuccCount == 0) {
                            if (tmpF_MSG.contains("查询外联记录为空") && canBeNotExist(p_reqQryBat, retAts, 30)) {
                                retAts.transState = bankHeader.TX_CODE + "+" + tmpF_STATUS + "+" + "查询外联记录为空";
                            } else {
                                retAts.transState = bankHeader.TX_CODE + "+" + tmpF_STATUS;
                            }
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                            retAts.payInfoCode = tmpF_STATUS;
                            retAts.respInfo = tmpF_MSG;
                            retAts.writeBeforeDetail();
                        } else if (tmpF_STATUS.equals("1")) {
//                            retAts.transState = bankHeader.TX_CODE + "+" + tmpF_STATUS;
                            retAts.respInfo = tmpF_MSG;
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
//                            retAts.payInfoCode = tmpF_STATUS;
                            retAts.writeBeforeDetail();
                            if (tmpSUCCESS_NUM != null && !tmpSUCCESS_NUM.equals("0")) {
                                tmpBkReader.resetReadMode(XmlReadMode.XElementReader);
                                while (tmpBkReader.read("/TX/TX_INFO/SUCCESS_LIST")) {
                                    RetAtsBatPayRecDetail tmpRetDetail = getSuccesListDetails(p_reqQryBat.reqHeader.transType, tmpBkReader.readedXElement);
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }

                            if (tmpFAIL_NUM != null && !tmpFAIL_NUM.equals("0")) {
                                tmpBkReader.resetReadMode(XmlReadMode.XElementReader);
                                while (tmpBkReader.read("/TX/TX_INFO/FAIL_LIST")) {
                                    RetAtsBatPayRecDetail tmpRetDetail = getFailListDetails(p_reqQryBat.reqHeader.transType, tmpBkReader.readedXElement);
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.transState = "";
                            retAts.payInfoCode = "";
                            if (StringHelper.hasAnyChar(tmpF_MSG)) {
                                retAts.respInfo = tmpF_MSG;
                            } else {
                                retAts.respInfo = "异常，请联系技术人员或银行！";
                            }
                        }
                    } else if (RETURN_CODE.equals("0130Z110BF34")) {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                        retAts.transState = bankHeader.TX_CODE + "+" + RETURN_CODE;
                        retAts.payInfoCode = RETURN_CODE;
                        String MESSAGE = tmpBkReader.getFirstText("/TX/TX_INFO/MESSAGE");
                        if (StringHelper.hasAnyChar(MESSAGE)) {
                            retAts.respInfo = MESSAGE;
                        } else {
                            retAts.respInfo = RETURN_MSG;
                        }
                        retAts.writeBeforeDetail();
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = RETURN_MSG;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
        }
        if (retAts.respCode.equals("-1")) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    // 成功明细
    private RetAtsBatPayRecDetail getSuccesListDetails(String p_transType, XElement p_xe) throws Exception {
        RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
        if (p_transType.equals("1998")) {
            tmpRetDetail.recAct = p_xe.getSingleText("RECV_ACCNO");
            tmpRetDetail.recName = p_xe.getSingleText("RECV_ACCNAME");
        } else if (p_transType.equals("9198")) {
            tmpRetDetail.payAct = p_xe.getSingleText("PAY_ACCNO");
            tmpRetDetail.payName = p_xe.getSingleText("PAY_ACCNAME");
        }

        tmpRetDetail.payAmount = p_xe.getSingleText("AMOUNT");
        tmpRetDetail.result = "1+S+" + p_xe.getSingleText("STATUS");
        if (StringHelper.isNullOrEmpty(p_xe.getSingleText("FILE_MEMO1"))) {
            tmpRetDetail.postScript = p_xe.getSingleText("USEOF");
        } else {
            tmpRetDetail.postScript = p_xe.getSingleText("FILE_MEMO1");
        }
        tmpRetDetail.usage = p_xe.getSingleText("FILE_MEMO2");//用途Usage
        tmpRetDetail.memo = p_xe.getSingleText("FILE_MEMO1") + p_xe.getSingleText("FILE_MEMO2"); //FILE_MEMO1,FILE_MEMO2
        tmpRetDetail.payInfoCode = p_xe.getSingleText("STATUS");
        return tmpRetDetail;
    }

    // 失败明细
    private RetAtsBatPayRecDetail getFailListDetails(String p_transType, XElement p_xe) throws Exception {
        RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
        if (p_transType.equals("1998")) {
            tmpRetDetail.recAct = p_xe.getSingleText("RECV_ACCNO");
            tmpRetDetail.recName = p_xe.getSingleText("RECV_ACCNAME");
        } else if (p_transType.equals("9198")) {
            tmpRetDetail.payAct = p_xe.getSingleText("PAY_ACCNO");
            tmpRetDetail.payName = p_xe.getSingleText("PAY_ACCNAME");
        }
        tmpRetDetail.payAmount = p_xe.getSingleText("AMOUNT");
        tmpRetDetail.result = "1+F+" + p_xe.getSingleText("STATUS");
        if (StringHelper.isNullOrEmpty(p_xe.getSingleText("FILE_MEMO1"))) {
            tmpRetDetail.postScript = p_xe.getSingleText("USEOF");
        } else {
            tmpRetDetail.postScript = p_xe.getSingleText("FILE_MEMO1");
        }
        tmpRetDetail.memo = p_xe.getSingleText("REMARK");
        String remark =  p_xe.getSingleText("REMARK");
        if (remark.contains(",")) {
            int index = remark.length();
            if(remark.indexOf(",") > 0) {
                index = index < remark.indexOf(",") ? index : remark.indexOf(",");
            }
            if(remark.indexOf("，") > 0){
                index = index < remark.indexOf("，") ? index : remark.indexOf("，");
            }

            tmpRetDetail.payInfoCode = remark.substring(0, index);
        } else {
            tmpRetDetail.payInfoCode = remark;
        }
        return tmpRetDetail;
    }
}